package com.liuning.graduationdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuning.graduationdemo.common.Result;
import com.liuning.graduationdemo.entity.User;
import com.liuning.graduationdemo.mapper.CourseMapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import com.liuning.graduationdemo.service.ICourseService;
import com.liuning.graduationdemo.entity.Course;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pumpkin
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private ICourseService courseService;

    @Resource
    private CourseMapper courseMapper;

    /**
     * 保存或更新
     * @param course
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Course course) {
        return Result.success(courseService.saveOrUpdate(course));
    }

    /**
     * 学生选课(先删除后增加)
     * @param courseId
     * @param studentId
     * @return
     */
    @PostMapping("/studentCourse/{courseId}/{studentId}")
    public Result studentCourse(@PathVariable(name = "courseId") Integer courseId,@PathVariable(name = "studentId") Integer studentId) {
        courseService.setStudentCourse(courseId,studentId);
        return Result.success();
    }

    /**
     * 删除单条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(courseService.removeById(id));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(courseService.list());
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseService.getById(id));
    }

    /**
     * 分页查找所以数据
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,
                           @RequestParam String name) {
//        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(Strings.isNotEmpty(name),"name",name);
        //关联查询课程对应的授课老师

        return Result.success(courseService.findPage(new Page<>(pageNum, pageSize),name));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        return Result.success(courseService.removeBatchByIds(ids));
    }


}

